#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys,os
lib_path = os.path.abspath('../../')
sys.path.append(lib_path)

from tools_karkkainen_sanders import *
from rstr_max import *
from tool_interval import *

str1 = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus imperdiet arcu ut dui molestie feugiat. Aliquam erat volutpat. Vivamus quis turpis tortor, non tristique ante. Etiam ullamcorper eros ut ante porta venenatis nec ac tortor. Aenean quis elit elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sagittis molestie consequat. Quisque posuere fringilla consequat. In vel lorem id erat pellentesque egestas ut eu risus. Donec quis dolor elit, id facilisis mauris. Nullam et velit in leo scelerisque ornare vel in eros.'''

str1_unicode = unicode(str1,'utf-8','replace')

rstr = Rstr_max()
rstr.add_str(str1_unicode)
r = rstr.go()

len_min, len_max = 5, 5
eff_min, eff_max = 0, 1000

len_min2, len_max2 = 3, 3
eff_min2, eff_max2 = 0, 1000

id_str_focus = 0

s1,s2 = Sequence([]),Sequence([])

for ((idStr, end), nb), (l, start_plage) in r.iteritems():
  ss = rstr.array_str[idStr][end-l:end]
  for o in range(start_plage, start_plage + nb) :
    offset, id_str = rstr.array_suffix[o]
    if(id_str == id_str_focus) :
      if(len_min <= l <= len_max and eff_min <= nb <= eff_max):
        s1.add_interval((offset,offset+l-1))
      if(len_min2 <= l <= len_max2 and eff_min2 <= nb <= eff_max2):
        s2.add_interval((offset,offset+l-1))

ts = Tool_sequencing()
u = [s1.union(), s2.union()]
t = [('<u>','</u>'), ('<b>','</b>')]
smart_r = ts.segment_text_multitag(str1_unicode, u, t)

print '<div style="font-size: 10pt;text-align:justify; font-family:Bitstream vera sans">'
print '<p>%s repeat len = 5 %s, %s repeat len=3 %s</p>'%(t[0][0], t[0][1], t[1][0], t[1][1])
print smart_r.encode('utf-8')
print '</div>'                      
